package com.micangcaihang.micang.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.utils.TimeUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.BusinessRemarkEntity;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/25
 *     update : 2017/07/25
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class BusinessRemarkAfapter extends BaseQuickAdapter<BusinessRemarkEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId The layout resource id of each item.
     * @param data        A new list is created out of this one to avoid mutable list
     */
    public BusinessRemarkAfapter(@LayoutRes int layoutResId, @Nullable List<BusinessRemarkEntity> data) {
        super(layoutResId, data);
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(BaseViewHolder helper, BusinessRemarkEntity item) {

        helper.setText(R.id.tv_realname, item.getRealname());
        helper.setText(R.id.tv_flow_step, item.getFlow_step());
        if (StringUtils.isEmpty(item.getFlow_detail())) {
            helper.setText(R.id.tv_detail, "");
        } else {
            helper.setText(R.id.tv_detail, item.getFlow_detail());
        }
        TextView tv_detail = helper.getView(R.id.tv_detail);
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        tv_detail.measure(width, height);
        int currentheight = tv_detail.getMeasuredHeight();
        View view = helper.getView(R.id.line);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ConvertUtils.dp2px(1), currentheight + ConvertUtils.dp2px(10));
        view.setLayoutParams(layoutParams);
        try {
            String minute = TimeUtils.millis2StringMinute(Long.parseLong(item.getFlow_time()) * 1000);
            helper.setText(R.id.tv_flow_time, minute);
        } catch (Exception e) {
            helper.setText(R.id.tv_flow_time, "");
        }
        if ("7".equals(item.getPosition_id())) {
            helper.setBackgroundRes(R.id.tv_realname, R.drawable.shape_business_remark_satrap);
        } else {
            helper.setBackgroundRes(R.id.tv_realname, R.drawable.shape_business_remark_staff);
        }


        if (helper.getLayoutPosition() == 0 && mData.size() == 0) {
            helper.setVisible(R.id.line, false);
        } else if (helper.getLayoutPosition() == 0 && mData.size() != 0) {
            helper.setVisible(R.id.line, true);
        } else if (helper.getLayoutPosition() == getItemCount() - 1 && mData.size() != 0) {
            helper.setVisible(R.id.line, false);
        } else {
            helper.setVisible(R.id.line, true);
        }

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp_89 = ConvertUtils.dp2px(89);
        int dp_22 = ConvertUtils.dp2px(22);
        if (helper.getLayoutPosition() == 0) {
            params.setMargins(dp_89, dp_22, dp_22, 0);
        } else if (helper.getLayoutPosition() == getItemCount() - 1) {
            params.setMargins(dp_89, 0, dp_22, dp_22);
        } else {
            params.setMargins(dp_89, 0, dp_22, 0);
        }
        helper.itemView.setLayoutParams(params);
    }
}
